.PHONY: check_lmmk
ifneq ($(wildcard .lm.mk),)
-include .lm.mk
else
check_lmmk:
	@echo "Please run 'make config'"
endif

# toolchain
CC_PREFIX ?= 
CC = $(CC_PREFIX)gcc
AS = $(CC_PREFIX)gcc -x assembler-with-cpp
CP = $(CC_PREFIX)objcopy
SZ = $(CC_PREFIX)size
OD = $(CC_PREFIX)objdump
HEX = $(CP) -O ihex
BIN = $(CP) -O binary -S

# build directory
BUILD_DIR := build

# import sub makefile file
-include arch/$(CONFIG_ARCH)/Makefile


$(BUILD_DIR):
	@mkdir $@


# Pseudo command
.PHONY: config distclean dtb clean flash


# Check if the proj.cfg file exists
config: proj.cfg
	@./lm.exe -c proj.cfg -f lm.cfg -o include/config.h -m 50
	@rm -rf test.exe $(BUILD_DIR)


# distclean command, delete build directory and proj.cfg file
distclean:
	@rm -rf $(BUILD_DIR) proj.cfg


# distclean clean
clean:
	@rm -rf test.exe $(BUILD_DIR) $(CONFIG_CHIP).dtb


# xxx_defconfig command, copy configs/xxx_defconfig into current directory
%_defconfig:
	@cp -ra configs/$(MAKECMDGOALS) proj.cfg
	@./lm.exe -c proj.cfg -f lm.cfg -o include/config.h
	@rm -rf test.exe $(BUILD_DIR)
	@(exit 0)


dtb: dts/$(CONFIG_CHIP).dts
	@echo "build $(CONFIG_CHIP).dtb"
	@./dtc.exe -I dts -O dtb -o dts/$(CONFIG_CHIP).dtb dts/$(CONFIG_CHIP).dts
